package com.aolong.archives.common.log;

import java.util.Map;

import org.hibernate.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志管理器
 * @author shilei
 *
 */
public class BaseLogger 
{
    private static BaseLogger global;   
    
    private Logger logger = null;
    
    public BaseLogger(Class<?> clazz)
    {
        this.logger = LoggerFactory.getLogger(clazz.getName());
    }
    
    public BaseLogger(String loggerName)
    {
        this.logger = LoggerFactory.getLogger(loggerName);
    }
    
    public void info(String msg)
    {
        if (this.logger != null)
        {
            this.logger.info(formatMsg(msg,"",""));
        }
    }
    
    public void info(String msg,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            String clzName = (clazz == null) ? "null" : clazz.getName();
            this.logger.info(formatMsg(msg,clzName,method));
        }
    }    
    
    public void info(String sql,Map<String,? extends Object> map,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            if (map == null)
            {
                this.info(sql,clazz,method);
            }
            else if (map.size() == 0)
            {
                this.info(sql,clazz,method);   
            }
            else
            {
                this.info(buildSql(sql,map),clazz,method);
            }
        }
    }
    
    public void debug(String msg)
    {
        if (this.logger != null)
        {        
            this.logger.debug(formatMsg(msg,"",""));
        }
    }
    
    public void debug(String msg,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            String clzName = (clazz == null) ? "null" : clazz.getName();
            this.logger.debug(formatMsg(msg,clzName,method));
        }
    }
    
    public void debug(String sql,Map<String,? extends Object> map,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            if (map == null)
            {
                this.debug(sql,clazz,method);
            }
            else if (map.size() == 0)
            {
                this.debug(sql,clazz,method);   
            }
            else
            {
                this.debug(buildSql(sql,map),clazz,method);
            }
        } 
    }
    
    public void error(String msg)
    {
        if (this.logger != null)
        {        
            this.logger.error(formatMsg(msg,"",""));
        }
    }
    
    public void error(String msg,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            String clzName = (clazz == null) ? "null" : clazz.getName();
            this.logger.error(formatMsg(msg,clzName,method));
        }
    }
    
    public void error(String sql,Map<String,? extends Object> map,Class<?> clazz,String method)
    {
        if (this.logger != null)
        {
            if (map == null)
            {
                this.error(sql,clazz,method);
            }
            else if (map.size() == 0)
            {
                this.error(sql,clazz,method);   
            }
            else
            {
                this.error(buildSql(sql,map),clazz,method);
            }
        } 
    }
    
    private String buildSql(String sql,Map<String,? extends Object> map)
    {
        
        for (String key : map.keySet())
        {
            String paraName = ":" + key;
            Object paraVal = map.get(key);
            String val = (paraVal == null) ? "" :  paraVal.toString();
            if (paraVal instanceof String || paraVal == null)
            {
                sql = StringHelper.replace(sql, paraName, "'" + val + "'");
            }
            else
            {
                sql = StringHelper.replace(sql, paraName, val);
            }
        }
        return sql;
    }
    
    private String formatMsg(String msg,String clzName,String method)
    {
        StringBuffer message = new StringBuffer();
        message.append(" class:"+clzName+"\r\n");
        message.append(" method:"+method+"\r\n");
        message.append(" info:"+msg);
        return message.toString();
    }
    
    static
    {
        if (global == null)
        {
            global = new BaseLogger(BaseLogger.class);
        }
    }
    /**
     * @return 全局默认记录器
     */
    public static BaseLogger global()
    {
        return global;
    }
    
    /**
     * 运行时自定义的记录器
     * @param loggerName
     * @return
     */
    public static BaseLogger userLogger(String loggerName)
    {
        return new BaseLogger(loggerName);
    }
    /**
     * 运行时自定义的记录器
     * @param loggerName
     * @return
     */    
    public static BaseLogger userLogger(Class<?> clazz)
    {
        return userLogger(clazz.getName());
    }    
}
